﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace DO.Workflows
{
    public class WorkflowTriggerInstructionBase:InstructionBase
    {
        #region props
        /// <summary>
        /// workflow can have maximum of one trigger
        /// all trigger must have associated workflow
        /// </summary>
        public bool IsActive { get; set; }
        public DateTime ActivatedOn { get; set; }
        public TimeSpan Span { get; set; }
        public DateTime? LastRunTime { get; set; }
        public DateTime? NextRunTime { get; set; }
        public TriggerType TriggerType { get; set; }
        #endregion

        #region ctor
        public WorkflowTriggerInstructionBase():base(Guid.Empty,Guid.Empty)
        {
            this.IsActive = false;
            this.ActivatedOn = DateTime.Now;
            this.Span = new TimeSpan(1, 0, 0, 0); // every day
            this.LastRunTime = null;
            this.NextRunTime = null;
            this.TriggerType = Workflows.TriggerType.ManualStart;
        }

        public WorkflowTriggerInstructionBase(
            Guid componentID, Guid ticketID, 
            bool isActive, DateTime activatedOn,
            TimeSpan span, DateTime? lastRunTime, DateTime? nextRunTime)
            :base(componentID, ticketID)
        {
            this.IsActive = isActive;
            this.ActivatedOn = activatedOn;
            this.Span = span;
            this.LastRunTime = lastRunTime;
            this.NextRunTime = nextRunTime;
        }
        #endregion

        public override XElement Serialize()
        {
            XElement xe = base.Serialize();
            xe.Add(
                new XAttribute("IsActive", this.IsActive.ToString()),
                new XAttribute("ActivatedOn", this.ActivatedOn.ToString()),
                new XAttribute("Span", this.Span.ToString()),
                new XAttribute("TriggerType",this.TriggerType.ToString()));
            if (this.LastRunTime.HasValue)
            {
                XAttribute lastRunAtt = null;
                lastRunAtt = new XAttribute("LastRunTime", this.LastRunTime.ToString());
                xe.Add(lastRunAtt);
            }
            if (this.NextRunTime.HasValue)
            {
                XAttribute nextRunAtt = null;
                nextRunAtt = new XAttribute("NextRunTime", this.NextRunTime.ToString());
                xe.Add(nextRunAtt);
            }
            return xe;
        }

        public override InstructionBase Instantiate(System.Xml.Linq.XElement xe)
        {
            WorkflowTriggerInstructionBase instruction = (WorkflowTriggerInstructionBase)base.Instantiate(xe);
            instruction.IsActive = bool.Parse(xe.Attribute("IsActive").Value);
            instruction.ActivatedOn = DateTime.Parse(xe.Attribute("ActivatedOn").Value);
            instruction.Span = TimeSpan.Parse(xe.Attribute("Span").Value);
            instruction.TriggerType = (TriggerType) Enum.Parse(typeof (TriggerType),
                                                               xe.Attribute("TriggerType").Value);
            if (xe.Attribute("LastRunTime") != null)
                instruction.LastRunTime = DateTime.Parse(xe.Attribute("LastRunTime").Value);
            if (xe.Attribute("NextRunTime") != null)
                instruction.NextRunTime = DateTime.Parse(xe.Attribute("NextRunTime").Value);
            return instruction;
        }
    }
}
